{extend name="common/__base" /}
{block name="body"}
<!-- table 放置 -->
<table id="table" lay-filter="table"></table>

<!--操作栏-->
<script id="operation" type="text/html">
    <button class="layui-btn layui-btn-xs" lay-event="status">标记已处理</button>
    <button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</button>
</script>

<!--工具栏-->
<script id="toolbar" type="text/html">
    <button class="layui-btn layui-btn-sm" lay-event="refresh"><i class="layui-icon layui-icon-refresh-3"></i></button>
    <div class="layui-inline">
        <input type="text" class="layui-input" id="search" placeholder="请输入关键词">
    </div>
    <button class="layui-btn layui-btn-sm" lay-event="search"><i class="layui-icon">&#xe615;</i></button>
</script>
{/block}

{block name="js"}
<script type="text/javascript">
	var table = layui.table;
	var form = layui.form;

	// 数据表格
    var dataTable = table.render({
        elem: "#table",
        url: "index",
        cols: [[ //表头
            {field: 'id', title: 'ID', sort: true},
            {field: 'username', title: '用户名'},
            {field: 'realname', title: '真实姓名'},
            {field: 'phone', title: '手机号'},
            {field: 'content', title: '反馈内容'},
            {field: 'status', title: '状态', templet:function(row){
            	if(row.status == 0){
            		return '待处理';
            	}else{
            		return '已处理';
            	}
            }},
            {field: 'create_time', title: '创建时间', sort: true},
            {title:'操作', toolbar: '#operation'}
        ]],
        response: {
            statusCode: 1 //规定成功的状态码，默认：0
        },
        parseData: function (ret) {
            return {
                code: ret.code,
                msg: ret.msg,
                count: ret.data.count,
                data: ret.data.data
            };
        },
        toolbar:'#toolbar',
        page:true,
        even: true
    });

    // 工具事件--编辑、删除
    table.on("tool(table)", function (obj) {
        switch (obj.event) {
            case 'status': // 审核
                showConfirm('确定标记为已处理吗？', {icon: 3, title: '标记为已处理', btn: ['确认', '取消']}, function () {
                    ajaxRequest('setStatus', {'id': obj.data.id})
                        .then(function (ret) {
                            showMsg(ret.msg)
                            if (ret.code == 1) {
                                location.reload();
                            }
                        })
                        .catch(function (err) {
                            console.log(JSON.stringify(err));
                        })
                });
                break;
            case 'del': // 删除
                showConfirm('确定删除吗？', {icon: 3, title: '删除反馈', btn: ['确认', '取消']}, function () {
                    ajaxRequest('delete', {'id': obj.data.id})
                        .then(function (ret) {
                            showMsg(ret.msg)
                            if (ret.code == 1) {
                                obj.del();//删除对应行（tr）的DOM结构，并更新缓存
                            }
                        })
                        .catch(function (err) {
                            console.log(JSON.stringify(err));
                        })
                });
            break;
        }
    });

    // 工具栏事件--添加、刷新、搜索
    table.on("toolbar(table)", function (obj) {
        switch (obj.event) {
            case 'refresh':
                location.reload();
                break;
            case 'search':
                var search = $('#search').val();
                if(!search){
                	showMsg('请输入关键词',{icon:2});
                	return false;
                }
                // table表格重载
                dataTable.reload({
                    where: {
                        search: search
                    },
                    page: {
                        curr: 1
                    }
                });
                $("#search").val(search);
                break;
        }
    });
</script>
{/block}
